﻿using System;
using System.Collections.Generic;
using FrankJie.Model;
using FrankJie.IDAL;
using FrankJie.Cache;

namespace FrankJie.BLL
{
    public class Link
    {
        private readonly static ILink dal = DataAccess.CreateLink();

        /// <summary>
        /// 增加一个友情链接
        /// </summary>
        /// <param name="linkInfo">友情链接实体</param>
        /// <returns>返回友情链接ID</returns>
        public static int Add(LinkInfo linkInfo)
        {
            int id = dal.Add(linkInfo);
            if (id > 0)
            {
                CacheStrategy.RemoveObject(CacheKey.LINK_LIST);
            }
            return id;
        }

        /// <summary>
        /// 更新一个友情链接
        /// </summary>
        /// <param name="linkInfo">友情链接实体</param>
        /// <returns>返回是否成功</returns>
        public static bool Update(LinkInfo linkInfo)
        {
            bool isSuccess = dal.Update(linkInfo);
            if (isSuccess)
            {
                CacheStrategy.RemoveObject(CacheKey.LINK_LIST);
            }
            return isSuccess;
        }

        /// <summary>
        /// 删除一个友情链接
        /// </summary>
        /// <param name="id">友情链接ID</param>
        /// <returns>返回是否成功</returns>
        public static bool Delete(int id)
        {
            bool isSuccess = dal.Delete(id);
            if (isSuccess)
            {
                CacheStrategy.RemoveObject(CacheKey.LINK_LIST);
            }
            return isSuccess;
        }

        /// <summary>
        /// 获取友情链接列表
        /// </summary>
        /// <returns>返回友情链接集合</returns>
        public static List<LinkInfo> GetList()
        {
            List<LinkInfo> linkInfoList = new List<LinkInfo>();
            linkInfoList = (List<LinkInfo>)CacheStrategy.GetObject(CacheKey.LINK_LIST);
            if (linkInfoList == null)
            { 
                linkInfoList = dal.GetList();
                CacheStrategy.AddObject(CacheKey.LINK_LIST, linkInfoList);
            }
            return linkInfoList;
        }


    }
}
